<?php
/**
 * Egany_Admin_Meta_Boxes 
 *
 * Author URI: http://www.facebook.com/
 * @author 		PhongND
 * @category 	Admin
 * @package 	egany_discount_offer
 * @version     1.0
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly
}


class Egany_Admin_Meta_Boxes {

	private static $meta_box_errors = array();
	private static $post_types_available = array('periodic_discount', '');
	private static $pt_periodic_discount = 'periodic_discount';
	
	/**
	 * Constructor
	 */
	public function __construct() {
		// output form: 
		add_filter( 'manage_edit-periodic_discount_columns', array( $this, 'periodic_discount_columns' ) );
		add_filter( 'manage_edit-validation_period_columns', array( $this, 'validation_period_columns' ) );
		add_action( 'manage_periodic_discount_posts_custom_column', array( $this, 'render_periodic_discount_columns' ), 2 );
		add_action( 'manage_validation_period_posts_custom_column', array( $this, 'render_validation_period_columns' ), 2 );
		
		// input form: 
		include_once('egany-metabox-periodic-discount.php'); // static functions for periodic-discount
		include_once('egany-metabox-validation-period.php'); // static functions for periodic-discount
		include_once('egany-meta-box-periodic-discount-items.php'); // static functions for periodic-discount
		// add_action( 'add_meta_boxes', array( $this, 'remove_meta_boxes' ), 10 ); // nono ???
		add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes'),30 ); // 
		add_action( 'save_post', array( $this, 'save_meta_boxes'), 1, 2  ); //

		/**
		 * Save Meta Boxes
		 *		periodic_discount
		 *		validation_period
		 *		
		 */
		add_action( 'egany_save_periodic_discount_meta', 'Egany_Metabox_Periodic_Discount::save', 40, 2 ); // nono: , 40, 2...30, 2; ...  50, 2; ...
		add_action( 'egany_save_validation_period_meta', 'Egany_Metabox_Validation_Period::save', 40, 2 );//, 40, 2 


		// // Error handling (for showing errors from meta boxes on next page load)
		// add_action( 'admin_notices', array( $this, 'output_errors' ) );
		// add_action( 'shutdown', array( $this, 'save_errors' ) );
	}

	/**
	 * Add an error message
	 * @param string $text
	 */
	// public static function add_error( $text ) {
		// self::$meta_box_errors[] = $text;
	// }

	/**
	 * Save errors to an option
	 */
	// public function save_errors() {
		// update_option( 'woocommerce_meta_box_errors', self::$meta_box_errors );
	// }

	/**
	 * Show any stored error messages.
	 */
	// public function output_errors() {
		// $errors = maybe_unserialize( get_option( 'woocommerce_meta_box_errors' ) );

		// if ( ! empty( $errors ) ) {

			// echo '<div id="woocommerce_errors" class="error fade">';
			// foreach ( $errors as $error ) {
				// echo '<p>' . esc_html( $error ) . '</p>';
			// }
			// echo '</div>';

			// // Clear
			// delete_option( 'woocommerce_meta_box_errors' );
		// }
	// }

	
	/**
	 * Add WC Meta boxes
	 */
	public function add_meta_boxes( ) { //$post_type, $post
	// Periodic Discount
		add_meta_box( 'egany-periodic-discount-data', __( 'Periodic Discount', 'EGANY' ), 'Egany_Metabox_Periodic_Discount::output', 'periodic_discount', 'normal', 'high' );
		// add_meta_box( 'egany-periodic-discount-items-data', __( 'Periodic Discount Items', 'EGANY' ), 'Egany_Metabox_Periodic_Discount_Items::output', 'periodic_discount', 'normal', 'high' );
		add_meta_box( 'egany-validation-period-data', __( 'Validation Period', 'EGANY' ), 'Egany_Metabox_Validation_Period::output', 'validation_period', 'normal', 'high' );
		
	}

	/**
	 * Remove bloat
	 */
	// public function remove_meta_boxes() {
		// remove_meta_box( 'product_shipping_classdiv', 'product', 'side' ); //nono
		// remove_meta_box( 'woothemes-settings', $type, 'normal' );//nono
		
	// }

	// /**
	 // * Rename core meta boxes
	 // */
	// public function rename_meta_boxes() {
		// global $post;

		// // Comments/Reviews
		// if ( isset( $post ) && ( 'publish' == $post->post_status || 'private' == $post->post_status ) ) {
			// remove_meta_box( 'commentsdiv', 'product', 'normal' );

			// add_meta_box( 'commentsdiv', __( 'Reviews', 'EGANY' ), 'post_comment_meta_box', 'product', 'normal' );
		// }
	// }

	/**
	 * Check if we're saving, the trigger an action based on the post type
	 *
	 * @param  int $post_id
	 * @param  object $post
	 */
	public function save_meta_boxes( $post_id, $post ) {
		// $post_id and $post are required
		if ( empty( $post_id ) || empty( $post ) ) {
			return;
		}

		// Dont' save meta boxes for revisions or autosaves
		if ( defined( 'DOING_AUTOSAVE' ) || is_int( wp_is_post_revision( $post ) ) || is_int( wp_is_post_autosave( $post ) ) ) {
			return;
		}

		// // Check the nonce
		// if ( empty( $_POST['woocommerce_meta_nonce'] ) || ! wp_verify_nonce( $_POST['woocommerce_meta_nonce'], 'woocommerce_save_data' ) ) {
			// return;
		// }

		// Check the post being saved == the $post_id to prevent triggering this call for other save_post events
		if ( empty( $_POST['post_ID'] ) || $_POST['post_ID'] != $post_id ) {
			return;
		}

		// Check user has permission to edit
		if ( ! current_user_can( 'edit_post', $post_id ) ) {
			return;
		}

		// // Check the post type
		// // if ( in_array( $post->post_type, wc_get_order_types( 'order-meta-boxes' ) ) ) {
		// if ( in_array( $post->post_type, self::$post_types_available ) ) {
			// do_action( 'egany_save_periodic_discount_meta', $post_id, $post );
		// } 
		
		// do_action( 'egany_save_periodic_discount_meta', $post_id, $post );
		do_action( 'egany_save_' . $post->post_type . '_meta', $post_id, $post );
		
		
		// elseif ( in_array( $post->post_type, array( 'product', 'periodic_discount' ) ) ) {
			// do_action( 'woocommerce_process_' . $post->post_type . '_meta', $post_id, $post );
		// }
		
	}
	
	
	

	/**
	 * Define custom columns for Periodic Discount
	 * @param  array $existing_columns
	 * @return array
	 */
	public function periodic_discount_columns( $existing_columns ) {
		$columns                = array();
		$columns["cb"]          = "<input type=\"checkbox\" />";
		$columns["periodic_no"] = __( 'No', 'EGANY' );
		$columns["description"]      = __( 'Description', 'EGANY' );
		$columns["priority"]      = __( 'Priority', 'EGANY' );
		$columns["discount_percent"]      = __( 'Discount Percent Value', 'EGANY' );
		$columns["discount_amount"]      = __( 'Discount Amount Value', 'EGANY' );
		$columns["starting_date"]      = __( 'Starting Date', 'EGANY' );
		return $columns;
	}

	
	/**
	 * Render No.
	 * @param  string $column
	 */
	public function render_no_output( $post,  $strColumn, $strPostSingularName) {
		
		// echo esc_html( get_post_meta( $post->ID, 'periodic_no', true ) );
		
		$edit_link = get_edit_post_link( $post->ID );
		$title = get_post_meta( $post->ID, $strColumn, true ) ; //_draft_or_post_title();
		$post_type_object = get_post_type_object( $post->post_type );
		$can_edit_post = current_user_can( $post_type_object->cap->edit_post, $post->ID );

		echo '<div class="code tips" data-tip="' . __( 'Edit '.$strPostSingularName, 'EGANY' ) . '"><a href="' . esc_attr( $edit_link ) . '"><span>' . esc_html( $title ). '</span></a></div>';

		_post_states( $post );

		// Get actions
		$actions = array();

		if ( current_user_can( $post_type_object->cap->delete_post, $post->ID ) ) {
			if ( 'trash' == $post->post_status )
				$actions['untrash'] = "<a title='" . esc_attr( __( 'Restore this item from the Trash', 'EGANY' ) ) . "' href='" . wp_nonce_url( admin_url( sprintf( $post_type_object->_edit_link . '&amp;action=untrash', $post->ID ) ), 'untrash-post_' . $post->ID ) . "'>" . __( 'Restore', 'EGANY' ) . "</a>";
			elseif ( EMPTY_TRASH_DAYS )
				$actions['trash'] = "<a class='submitdelete' title='" . esc_attr( __( 'Move this item to the Trash', 'EGANY' ) ) . "' href='" . get_delete_post_link( $post->ID ) . "'>" . __( 'Trash', 'EGANY' ) . "</a>";
			if ( 'trash' == $post->post_status || !EMPTY_TRASH_DAYS )
				$actions['delete'] = "<a class='submitdelete' title='" . esc_attr( __( 'Delete this item permanently', 'EGANY' ) ) . "' href='" . get_delete_post_link( $post->ID, '', true ) . "'>" . __( 'Delete Permanently', 'EGANY' ) . "</a>";
		}

		$actions = apply_filters( 'post_row_actions', $actions, $post );

		echo '<div class="row-actions">';

		$i = 0;
		$action_count = sizeof($actions);

		foreach ( $actions as $action => $link ) {
			++$i;
			( $i == $action_count ) ? $sep = '' : $sep = ' | ';
			echo "<span class='$action'>$link$sep</span>";
		}
		echo '</div>';
	}
	
	
	/**
	 * Output custom columns for Periodic Discount 
	 * @param  string $column
	 */
	public function render_periodic_discount_columns( $column ) {
		global $post; // , $woocommerce
		switch ( $column ) {
			case "periodic_no" :
				$this->render_no_output($post, 'periodic_no', 'Periodic Discount');
				break;
			case "description" :
				echo esc_html( get_post_meta( $post->ID, 'description', true ) );
				break;
			case "priority" :
				echo esc_html( get_post_meta( $post->ID, 'priority', true ) );
				break;
			case "discount_percent" :
				echo esc_html( get_post_meta( $post->ID, 'discount_percent', true ) );
				break;
			case "discount_amount" :
				echo esc_html( get_post_meta( $post->ID, 'discount_amount', true ) );
				break;
			case "starting_date" :
				echo esc_html( get_post_meta( $post->ID, 'starting_date', true ) );
				break;
			// case "customer_user" :
				// // echo esc_html( get_post_meta( $post->ID, 'customer_user', true ) );
				// echo wp_kses_post( $post->post_title );
				// break;
		}
	}
	
	
	
	/**
	 * Define custom columns for Validation Period
	 * @param  array $existing_columns
	 * @return array
	 */
	public function validation_period_columns( $existing_columns ) {
		$columns                = array();
		$columns["cb"]          = "<input type=\"checkbox\" />";
		$columns["validation_no"] = __( 'No', 'EGANY' );
		$columns["description"]      = __( 'Description', 'EGANY' );
		$columns["starting_date"]      = __( 'Starting Date', 'EGANY' );
		$columns["ending_date"]      = __( 'Ending Date', 'EGANY' );
		return $columns;
	}
	
	/**
	 * Output custom columns for Validation Period
	 * @param  string $column
	 */
	public function render_validation_period_columns( $column ) {
		global $post; // , $woocommerce
		switch ( $column ) {
			case "validation_no" :
				$this->render_no_output($post, 'validation_no', 'Validation Period');
				break;
			case "description" :
				echo esc_html( get_post_meta( $post->ID, 'description', true ) );
				break;
			case "starting_date" :
				echo esc_html( get_post_meta( $post->ID, 'starting_date', true ) );
				break;
			case "ending_date" :
				echo esc_html( get_post_meta( $post->ID, 'ending_date', true ) );
				break;
		}
	}
}

new Egany_Admin_Meta_Boxes();



